'''
Created on Aug 30, 2012

@author: kingsfield
'''

wordmap = dict()
itemmap = dict()
catmap = dict()
catmapinv = dict()
itemmapinv = dict()
wordmapinv = dict()

word_sim = dict()
word_item_score = dict()
word_count = dict()
word_item_count = dict()
cat_word_map = dict()
cat_item_map = dict()
cat_item_count = dict()
cat_hot = dict()

class Idmap(object):
    '''
    classdocs
    '''
    @classmethod
    def load(cls):
        with open('/home/kingsfield/Desktop/kaggle/kaggle/idmap/wordmap') as fr:
            data = fr.readlines()
            for line in data:
                ss = line.split(' ')
                wordmap[ss[0]] = int(ss[1])
                wordmapinv[int(ss[1])] = ss[0]
        with open('/home/kingsfield/Desktop/kaggle/kaggle/idmap/itemmap') as fr:
            data = fr.readlines()
            for line in data:
                ss = line.split(' ')
                itemmap[ss[0]] = int(ss[1])
                itemmapinv[int(ss[1])] = ss[0]
        with open('/home/kingsfield/Desktop/kaggle/kaggle/idmap/categorymap') as fr:
            data = fr.readlines()
            for line in data:
                ss = line.split(' ')
                catmap[ss[0]] = int(ss[1])
                catmapinv[int(ss[1])] = ss[0]
        with open('/home/kingsfield/Desktop/kaggle/kaggle/idmap/catwordmap') as fr:
            data = fr.readlines()
            for line in data:
                ss = line.split(' ')
                cid = int(ss[0])
                wid = int(ss[1])
                if not cid in cat_word_map:
                    cat_word_map[cid] = set()
                cat_word_map[cid].add(wid) 
        with open('/home/kingsfield/Desktop/kaggle/kaggle/idmap/catitemmap') as fr:
            data = fr.readlines()
            for line in data:
                ss = line.split(' ')
                cid = int(ss[0])
                iid = int(ss[1])
                count = int(ss[2])
                if not cid in cat_item_map:
                    cat_item_map[cid] = set()
                    cat_item_count[cid] = dict()
                cat_item_map[cid].add(iid)
                cat_item_count[cid][iid] = count
                
        for cid in cat_item_count:
            items = cat_item_count[cid].items()
            items = [(itemmapinv[i[0]], i[1]) for i in items]
            hot = sorted(items, key=lambda x:x[1], reverse=True)
            cat_hot[cid] = hot[0:5] if len(hot) > 5 else hot
